		
XfMissionAirSupport = {
	private ["_sleep","_type", "_number", "_current_target_pos","_current_radius", "_pos", "_town_location","_wait_counter", "_pilot", "_town_name", "_wp_behave", "_heli_type", "_vehicle", "_initial_type", "_grp", "_vehicles", "_funits", "_unit", "_num_p", "_re_random", "_grpskill", "_numair", "_xxx", "_vec_array", "_old_target", "_loop_do", "_dummy", "_current_target_pos", "_wp", "_pat_pos", "_radius", "_old_pat_pos", "_angle", "_x1", "_y1", "_vehicles_number", "_i", "_vecx", "_vec","_patternpos","_marker_name"];
	if (!isServer) exitWith {};
	_marker_name = "";
	if (d_mission_debug_messages) then {
		_marker_name = Format["airsuport%1",ceil(random 10)];
		_marker_name = [_marker_name, [0,0,0],"ICON","ColorRed",[2,2],"Target",0,"o_unknown"] call XfCreateMarkerGlobal;
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"mission_airsupport.sqf","Starting",_this, _marker_name];
	};

	_patternpos = {
		private ["_angle","_x1","_y1","_current_target_pos","_radius","_pat_pos"];
		_current_target_pos = _this select 0;
		_radius = _this select 1;
		_angle = floor (random 360);
		_x1 = (_current_target_pos select 0) - ((random _radius) * sin _angle);
		_y1 = (_current_target_pos select 1) - ((random _radius) * cos _angle);
		_pat_pos = [_x1, _y1,(_current_target_pos select 2)];
		_pat_pos;
	};
	_side = _this select 0;
	_current_target_pos = _this select 1;
	_multiplier = DM_Mission_AirSupport;
	If (count _this > 2) then {_multiplier = _this select 2;};
	
	_pos = [];
	if (count _this > 3) then { _pos = _this select 3; };
	if (isNil "_pos") then { _pos = []; };
	if (format["%1",_pos] == "[0,0,0]" || count _pos < 3) then { _pos = []; };

	_current_radius = 200;
	if (typename (_current_target_pos select 0) == "ARRAY") then {
		_current_radius = _current_target_pos select 1;
		_current_target_pos = _current_target_pos select 0;
	};
	_side_char = switch (_side) do {case "EAST": {"E"};case "WEST": {"W"};case "GUER": {"G"};case "CIV": {"W"}; default{"E"};};
	// TODO: Randomize the addition of airplanes
	_vehicle_array = missionNamespace getVariable format ["d_air_support_h_%1",_side_char];
	
	_wp_behave = "AWARE";

	_amount = 1;
	if ((call XPlayersNumber) > 10) then { _amount = 2 };
	_amount = _amount * _multiplier;
	If (_amount < 1 and _amount > 0) then {
		if (_amount <= (random 1)) then {
			_amount = 1;
		};
	} else {
		_amount = round (_amount);
	};
	If (_amount < 1) exitWith { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"mission_airsupport.sqf","No air support this time",_amount];};
	
	_grp = objNull;
	_vehicle = objNull;
	_vehicles = [];
	_funits = [];
	_unit = objNull;

	if (count _pos < 3) then {
		_pos = call XfGetRanPointOuterAir;
		_lhd = FLAG_BASE;
		If (!isnil "LHD_CENTER") then {_lhd = LHD_CENTER;};
		while {(_pos distance FLAG_BASE < 2500) || (_pos distance _lhd < 2500)} do {
			_pos = call XfGetRanPointOuterAir;
			sleep 0.01;
		};
	};
	_grpskill = 0.6 + (random 0.3);
	
	_numair = _amount;

	_cdir = [_pos, d_island_center] call XfDirTo;
	// We get a number of air vehicles between 1 and the max number to create.
	If (_numair > 1) then {	_numair = 1 + round(random (_numair - 1));};
	if (d_mission_debug_messages) then {
		diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"mission_airsupport.sqf","Randomized Num Air", _numair, _amount];
	};
	_grp = [_side] call x_creategroup;
	_grp setFormation "VEE";
	_grp setBehaviour "CARELESS";
	_grp setCombatMode "RED";
	_grp setSpeedMode "FULL";
	_grp allowFleeing 0;
	// We instantiate all units and add them to the group.
	for "_xxx" from 1 to _numair do {
		_heli_type = _vehicle_array call XfRandomArrayVal;
		// We correct the Z position so that it doesn't hit anything :-)
		_pos set [2,(50 + (_xxx * 20))];
		// We correct the Y position to give some space between the units.
		_pos set [1,((_pos select 1) + 60)];
		_vehicle = [_heli_type,_grp,[_pos,_cdir],"REGISTER",[],"","FLY"] call XfMissionCreateMissionUnit;
	};
	_i = 0;
	_units = units _grp;
	if (d_mission_debug_messages) then {
		diag_log Format["(%1)[%2]: %3: type:%4  grp:%5  units:%6(%7)  target:%8",diag_tickTime,"mission_airsupport.sqf","Creation",typeof _vehicle, _grp, _units, typeName _units, _current_target_pos];
	};
	{
		if ((typeof _x) isKindOf "Man") then {
			_vehicle = vehicle _x;
			if (!(_vehicle in _vehicles)) then {
				_vehicles set [count _vehicles, _vehicle];
				_vehicle flyInHeight 200;
				If (_heli_type isKindOf "Plane") then { _vehicle flyInHeight 400; };
				_marker_text = localize "STR_MF_UNKNOWNAIRCONTACT";
				_marker_color = "ColorRed";
				if (_side == d_own_side) then {
					_marker_text = localize "STR_MF_ALLIEDAIRCONTACT";
					_marker_color = "ColorGreen";
				};
				// [_vehicle,_marker_text,_marker_color] spawn XfAirMarkerMove;
				[(group (driver _vehicle)), _marker_text,_marker_color,"Air","WithAntiAirRadar"] spawn XfGroupMarkerMove;
				sleep 0.1;
				if (d_mission_debug_messages) then {
					diag_log Format["(%1)[%2]: %3: type:%4  grp:%5  driver:%6  crew:%7",diag_tickTime,"mission_airsupport.sqf","Spawned vehicle",typeof _vehicle, _grp, str(driver _vehicle), count(crew _vehicle)];
				};
			};
			_funits set [count _funits, _x]
		};
		diag_log Format["(%1)[%2]: %3: %4 - %5 (%6)",diag_tickTime,"mission_airsupport.sqf","Units",_x,typeof _x, vehicle _x];
	} foreach _units;
	sleep 1.011;
	if (count (waypoints _grp) > 1) then {
		while {(count (waypoints _grp) > 1)} do {
			deleteWaypoint [_grp, 1];
		};
		// _wp = [_grp, 1];
		// _wp setWaypointPosition [_current_target_pos, 0];
	// } else {
	};
	_wp = _grp addWayPoint [_current_target_pos, 0];
	if (d_mission_debug_messages) then {
		diag_log Format["(%1)[%2]: %3: type:%4  grp:%5  cnt:%6  pos1:%7",diag_tickTime,"mission_airsupport.sqf","Waypoints",typeof _vehicle, _grp, count (waypoints _grp),  waypointPosition _wp];
	};	
	_wp setWaypointType "SAD";
	_pat_pos = _current_target_pos;
	_wait_counter = 0;
	[_grp, 1] setWaypointStatements ["never", ""];
	if (d_mission_debug_messages) then {
		_marker_name setMarkerPos _current_target_pos;
		_marker_name setMarkerText "Current Target";
	};
	_last_wp_pos = [0,0,0];
	_loop_do = true;
	while {_loop_do} do {
		// We take the first vehicle in the list. If one is destroyed, there will always be a first one.
		_vehicle = _vehicles select 0;
		_pilot = driver _vehicle;
		If ((behaviour _pilot) == "COMBAT") then { _wait_counter = 0 };
		_isplane = (_vehicle isKindof "plane");
		// Calculate the radius which sets when the destination is reached
		sleep 3 + random 2;
		_radius = _current_radius * 3;
		if (_isplane) then {_radius = _current_radius * 5;};
		
		if (_vehicle distance _current_target_pos < _radius) then {
			if (!_isplane) then {
				_old_pat_pos = _pat_pos;
				// __patternpos;
				_pat_pos = [_current_target_pos,_radius] call _patternpos;

				while {_pat_pos distance _old_pat_pos < 100} do {
					// __patternpos;
					_pat_pos = [_current_target_pos,_radius] call _patternpos;
					sleep 0.01;
				};
				_pat_pos = _pat_pos call XfWorldBoundsCheck;
				[_grp, 1] setWaypointPosition [_pat_pos, 0];
				_grp setSpeedMode "NORMAL";
				_grp setBehaviour _wp_behave;
				if (d_mission_debug_messages) then {
					_marker_name setMarkerPos _pat_pos;
					_marker_name setMarkerText "Pattern Target";
					diag_log Format["(%1)[%2]: %3 to %4",diag_tickTime,"mission_airsupport.sqf",_marker_name,_pat_pos];
				};
				sleep 45.821 + random 15;
			} else {
				// __patternpos;
				_pat_pos = [_current_target_pos,_radius] call _patternpos;
				_pat_pos = _pat_pos call XfWorldBoundsCheck;
				[_grp, 1] setWaypointPosition [_pat_pos, 0];
				_grp setSpeedMode "LIMITED";
				_grp setBehaviour _wp_behave;
				if (d_mission_debug_messages) then {
					_marker_name setMarkerPos _pat_pos;
					_marker_name setMarkerText "Pattern Target";
				};
				sleep 60 + random 20;
			};
			_wait_counter = _wait_counter + 1;
		} else {
			// We update the way point in case it changed.
			If (Format["%1",_last_wp_pos] != Format["%1",_current_target_pos]) then {
				[_grp, 1] setWaypointPosition [_current_target_pos, 0];
				_last_wp_pos = _current_target_pos;
				if (d_mission_debug_messages) then {
					diag_log Format["(%1)[%2]: %3 heading to %4 (%5, %6)",diag_tickTime,"mission_airsupport.sqf",typeof _vehicle,_current_target_pos, getpos _vehicle,_vehicle distance _current_target_pos];
				};
			};
			if (d_mission_debug_messages) then {
				_marker_name setMarkerPos _current_target_pos;
				_marker_name setMarkerText "Current Target";
			};
			If (_isplane) then { _vehicle flyInHeight 400; };
		};

		// This part will refuel the vehicle or delete it if destroyed
		if (count _vehicles > 0) then {
			_vehicles_number = count _vehicles;
			for "_i" from 0 to _vehicles_number do {
				_vecx = _vehicles select _i;
				if (fuel _vecx < 0.1 && canMove _vecx) then {
					if (d_mission_debug_messages) then {
						diag_log Format["(%1)[%2]: %3 (%4) %5",diag_tickTime,"mission_airsupport.sqf","Vehicle out of fuel", typeof(_vecx) ,[fuel _vecx , canMove _vecx]];
					};
					_vecx spawn {
						private "_vec";
						_vec = _this;
						_vec setDamage 1.1;
						sleep 200;
						if (!isNull _vec) then {_vec call XfDelVecAndCrew};
					};
					_vehicles set [_i, -1];
				} else {
					if (isNull _vecx || !alive _vecx || !canMove _vecx) then {
						_vehicles set [_i, -1];
						if (d_mission_debug_messages) then {
							diag_log Format["(%1)[%2]: %3 (%4) %5",diag_tickTime,"mission_airsupport.sqf","Deleted vehicle", typeof(_vecx) ,[isNull _vecx , !alive _vecx , !canMove _vecx]];
						};
					} else {
						_vecx setFuel 1;
					};
				};
				sleep 0.01;
			};
			_vehicles = _vehicles - [-1];
		};
		if (count _vehicles == 0) then {
			{if (!isNull _x) then {_x setDamage 1; sleep 0.01; deleteVehicle _x}} forEach _funits;
			if (d_mission_debug_messages) then {
				diag_log Format["(%1)[%2]: %3",diag_tickTime,"mission_airsupport.sqf","All vehicles destroyed"];
			};
			_funits = [];
			_vehicles = [];
			_loop_do = false;
		};
	};
	if (d_mission_debug_messages) then {
		diag_log Format["(%1)[%2]: %3",diag_tickTime,"mission_airsupport.sqf","Exited the loop"];
		// We exited, this means that the vehicle is destroyed or else
		_marker_name setMarkerPos [0,0,0];
		_marker_name setMarkerText "";
		deleteMarkerLocal _marker_name;
	};
};